package com.vxb.testmvp.test_fragment.fm_b;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.design.widget.TabLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vxb.testmvp.R;
import com.vxb.testmvp.test_fragment.BaseFragment;
import com.vxb.testmvp.test_fragment.LoadingPager;
import com.vxb.testmvp.test_fragment.fm_c.FragmentC;
import com.vxb.testmvp.test_fragment.fm_a.FragmentA;

import java.util.ArrayList;

/**
 * Created by User on 2017/3/1.
 */

public class FragmentB extends BaseFragment {
    private View view;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<View> mList;
    private String[] mTitle;
    private ArrayList<BaseFragment> mFmList;



    private void initVpFmData(LayoutInflater inflater) {

        mFmList = new ArrayList<>();
        mFmList.add(new FragmentA());
        mFmList.add(new FragmentA());
        mFmList.add(new FragmentC());

    }

    private void initVpData(LayoutInflater inflater) {
        View viewpagerA = inflater.inflate(R.layout.viewpager, null);
        View viewpagerB = inflater.inflate(R.layout.viewpager, null);
        View viewpagerC = inflater.inflate(R.layout.viewpager, null);

        mList = new ArrayList<>();
        mList.add(viewpagerA);
        mList.add(viewpagerB);
        mList.add(viewpagerC);

        mTitle = new String[]{"首页", "分类", "设置"};
    }

    @Override
    protected View createSuccessView() {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_b, null);
        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);

        initVpData(LayoutInflater.from(getActivity()));
        initVpFmData(LayoutInflater.from(getActivity()));

        FragmentManager fragmentManager = getActivity().getFragmentManager();

        MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
        MyFmPagerAdapter myFmPagerAdapter = new MyFmPagerAdapter(fragmentManager);
        MyFmStatePagerAdapter myFmStatePagerAdapter = new MyFmStatePagerAdapter(fragmentManager);
        viewPager.setAdapter(myFmStatePagerAdapter);
        viewPager.setOffscreenPageLimit(3);

        tabLayout.setupWithViewPager(viewPager);
        return view;
    }

    @Override
    protected LoadingPager.LoadResult load() {
        return LoadingPager.LoadResult.SUCCESS;
    }

    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }

    class MyFmPagerAdapter extends FragmentPagerAdapter {

        public MyFmPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFmList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mFmList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }
    }

    class MyFmStatePagerAdapter extends FragmentStatePagerAdapter {

        public MyFmStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFmList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mFmList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }

    }
}
